﻿using System;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Framework.Model;
using MvcApplication1.Framework;
using MvcApplication1.Models;
using MvcApplication1.Services;

namespace MvcApplication1.Controllers
{
    public class ToDoController : Controller
    {
        public ActionResult Index()
        {
            var repository = new Repository(new ConnectionStringLoader(), new CacheWrapper());
            var todo = repository.FindAll<ToDo>();
            return Json(todo, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Save(ToDo toDo)
        {
            try
            {
                var repository = new Repository(new ConnectionStringLoader(), new CacheWrapper());
                repository.Save(toDo);
            }
            catch (Exception e)
            {
                return Json(new {Success = false, Error = e.Message});
            }
            return Json(new {Success = true, Model = new JavaScriptSerializer().Serialize(toDo)});
        }

    }
}
